<?php

class Db 
{
    private $m_beginTransaction;
    private $m_con;
    private $m_errorMessage;
    public function __construct()
    {
        $this->m_con = null;
        $this->m_beginTransaction = false;
        mb_language("uni");
        mb_internal_encoding("UTF-8");
    }
    
    public function __destruct() 
    {
        if (! is_null( $this->m_con ) && $this->m_beginTransaction)
            $this->m_con->rollBack();
            
        $this->m_con = null;
    }
    
    public function connection()
    {
        if (is_null( $this->m_con ))
            $this->connect();
        
        if (! is_null( $this->m_con ) && ! $this->m_beginTransaction )
        {
            $this->m_con->beginTransaction() ? $this->m_beginTransaction = true : $this->m_con = null;
        }
        
        return $this->m_con;
    }
    
    public function connectionNoTransaction()
    {
        if (is_null( $this->m_con ))
            $this->connect();

        return $this->m_con;
    }
    
    public function commit()
    {
        if (! is_null( $this->m_con ) && $this->m_beginTransaction)
        {
            if ($this->m_con->commit())
            {
                $this->m_beginTransaction = false;
                return true;
            }
        }
        
        return false;
    }
    
    public static function db()
    {
        return new Db;
    }
    
    public function getErrorStr()
    {
        return $this->m_errorMessage;
    }
    
    private function connect()
    {
        $this->m_con = null;
        $iniReader = new IniReader();
        if (! $iniReader->iniValue( "hostname", $hostname ))
        {
            $this->m_errorMessage = "Unknown hostname in the initialization file";
            return;
        }
        if (! $iniReader->iniValue( "dbname", $dbname ))
        {
            $this->m_errorMessage = "Unknown dbname in the initialization file";
            return;
        }
        if (! $iniReader->iniValue( "port", $port ))
        {
            $this->m_errorMessage = "Unknown password in the initialization file";
            return;
        }
        if (! $iniReader->iniValue( "username", $username ))
        {
            $this->m_errorMessage = "Unknown username in the initialization file";
            return;
        }
        
        if (! $iniReader->iniValue( "password", $password ))
        {
            $this->m_errorMessage = "Unknown password in the initialization file";
            return;
        }
        
        if (! isset($password))
            $password = "";

        //$options = array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
        try 
        {
            $connectionString = "pgsql:dbname=$dbname;host=$hostname;port=$port;user=$username;password=$password";
            $this->m_con = new PDO( $connectionString );
            $this->m_con->exec("SET CHARACTER SET utf8");
            $this->m_con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $this->m_errorMessage = "";
        }
        catch(PDOException $e)
        {
            $this->m_con = null;
            $this->m_errorMessage = $e->getMessage();
        }
    }
}

?>
